/*
 * @Author: 0x9DEFA478
 * @Date: 2021-08-27 13:39:06
 * @LastEditTime: 2021-09-28 21:26:28
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "GUI_User_Page.h"
#include "Peripheral.h"



static void Itemx_SelectCallback(H_StringItem_ListItem* Item){
  Item->String[0]='-';
  Item->String[1]='>';
}

static void Itemx_DeSelectCallback(H_StringItem_ListItem* Item){
  Item->String[0]=' ';
  Item->String[1]=' ';
}




static void ItemScreen_Light_AddClickCallback(H_StringItem_ListItem* Item){
  int Screen_Light;
  unsigned char strBuffer[32];

  Screen_Light=I_NVM.Screen_Light;

  if(Screen_Light<100){
    Screen_Light++;
  }

  H_TS_ThreadSleep(20);

  I_NVM_Locking();
  I_NVM.Screen_Light=Screen_Light;
  OLED_SetContrast(Screen_OLED1,Screen_Light);
  I_NVM_UnLock();

  H_StringItem_SetString(Item,H_String(strBuffer,"->屏幕亮度:{i}    ",I_NVM.Screen_Light));
}

static void ItemScreen_Light_SubClickCallback(H_StringItem_ListItem* Item){
  int Screen_Light;
  unsigned char strBuffer[32];

  Screen_Light=I_NVM.Screen_Light;

  if(Screen_Light>0){
    Screen_Light--;
  }

  H_TS_ThreadSleep(20);

  I_NVM_Locking();
  I_NVM.Screen_Light=Screen_Light;
  OLED_SetContrast(Screen_OLED1,Screen_Light);
  I_NVM_UnLock();

  H_StringItem_SetString(Item,H_String(strBuffer,"->屏幕亮度:{i}    ",I_NVM.Screen_Light));
}


static void ItemScreen_Direction_AddClickCallback(H_StringItem_ListItem* Item){
  int Screen_Direction;

  Screen_Direction=I_NVM.Screen_Direction;

  if(Screen_Direction==vOLED_Direction_0){
    Screen_Direction=vOLED_Direction_180;
  }else{
    Screen_Direction=vOLED_Direction_0;
  }

  H_TS_ThreadSleep(20);

  I_NVM_Locking();
  I_NVM.Screen_Direction=Screen_Direction;
  OLED_SetDirection(Screen_OLED1,Screen_Direction);
  I_NVM_UnLock();

  if(Screen_Direction==vOLED_Direction_0){
    H_StringItem_SetString(Item,"->屏幕方向:   正");
  }else{
    H_StringItem_SetString(Item,"->屏幕方向:   反");
  }

}




static void ListExitCallback(H_StringItem_List* List){
  H_UI_ExitSelect(List->UserInstence);
}


static H_StringItem* ItemInit(H_UI* h_ui){
  H_StringItem* r;
  H_StringItem_List* List;
  H_StringItem_ListItem* Item;

  unsigned char strBuffer[32];


  r=new_H_StringItem(GUI_User_DrawString,MemoryMalloc_Malloc,MemoryMalloc_Free);

  List=new_H_StringItem_List(r,0,16,128,48);
  List->UserInstence=h_ui;
  List->ExitCallback=ListExitCallback;

  H_StringItem_SetList(r,List);



  Item=H_StringItem_List_AddItem(List,16);
  H_StringItem_SetFont(Item,Font_LLib_Song);

  H_StringItem_SetString(Item,H_String(strBuffer,"  屏幕亮度:{i}    ",I_NVM.Screen_Light));
  Item->AddClickCallback=ItemScreen_Light_AddClickCallback;
  Item->SubClickCallback=ItemScreen_Light_SubClickCallback;
  Item->SelectCallback=Itemx_SelectCallback;
  Item->DeSelectCallback=Itemx_DeSelectCallback;


  Item=H_StringItem_List_AddItem(List,16);
  H_StringItem_SetFont(Item,Font_LLib_Song);

  if(I_NVM.Screen_Direction==vOLED_Direction_0){
    H_StringItem_SetString(Item,"  屏幕方向:   正");
  }else{
    H_StringItem_SetString(Item,"  屏幕方向:   反");
  }
  Item->AddClickCallback=ItemScreen_Direction_AddClickCallback;
  Item->SubClickCallback=ItemScreen_Direction_AddClickCallback;
  Item->SelectCallback=Itemx_SelectCallback;
  Item->DeSelectCallback=Itemx_DeSelectCallback;

  return r;
}




// static void BackCallback(void* page){
//   H_UI_Page* Page;
//   Page=page;
//   if(Page->UserInstence!=NULL){
//     H_StringItem_EventBack(Page->UserInstence);
//   }else{
//     H_UI_ExitSelect(Page->Parent);
//   }
// }

// static void EnterCallback(void* page){
//   H_UI_Page* Page;
//   Page=page;
//   if(Page->UserInstence!=NULL){
//     H_StringItem_EventEnter(Page->UserInstence);
//   }
// }

// static void AddCallback(void* page){
//   H_UI_Page* Page;
//   Page=page;
//   if(Page->UserInstence!=NULL){
//     H_StringItem_EventAdd(Page->UserInstence);
//   }
// }

// static void SubCallback(void* page){
//   H_UI_Page* Page;
//   Page=page;
//   if(Page->UserInstence!=NULL){
//     H_StringItem_EventSub(Page->UserInstence);
//   }
// }

// static void EnterPageCallback(void* page){
// }

// static void ExitPageCallback(void* page){
// }

static void DisplayPageCallback(void* page){
  H_UI_Page* Page;

  unsigned char strBuffer[32];
  char* str;
  int strWidth;
  int strHeight;

  Page=page;

  GUI_User_ClearBitmap(GUI_User_GetDrawStringBitmap());

  str="屏幕设置";
  H_Font_GetStringSize(Font_LLib_Song,str,&strWidth,&strHeight);
  GUI_User_DrawString(str,Font_LLib_Song,0,(128-strWidth)/2,0,127,63);

  if(((H_UI*)Page->Parent)->IsSelect==0){
    //无选中

    str="选中页面以设置";
    H_Font_GetStringSize(Font_LLib_Song,str,&strWidth,&strHeight);
    GUI_User_DrawString(str,Font_LLib_Song,0,(128-strWidth)/2,32,127,63);
  }else{
    //选中

    H_StringItem_Draw(Page->UserInstence);
  }
}

H_StringItem* GUI_User_Page_Screen_Init(H_UI* h_ui,H_UI_Callbacks* Callbacks){

  GUI_User_Page_GetDefaultCallbacks(Callbacks);
  Callbacks->DisplayPageCallback=DisplayPageCallback;

  return ItemInit(h_ui);
}

void GUI_User_Page_Screen_DeInit(H_StringItem* stringitem){
}
